package com.hsy.dormmanage.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsy.dormmanage.dao.CheckDao;
import com.hsy.dormmanage.dao.StudentDao;
import com.hsy.dormmanage.model.Student;
import com.hsy.dormmanage.model.vo.StudentVo;
import com.hsy.dormmanage.service.StudentSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 包名:com.hsy.dormmanage.service.impl
 * 用户:86182
 * 时间:20211110:452021/11/9
 */
@Service
public class StudentSerImpl extends ServiceImpl<StudentDao,Student> implements StudentSer {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private CheckDao checkDao;

    @Override
    public Student queryById(long id, String pwd) {
        Student student = studentDao.queryById(id, pwd);
        return student;
    }

    @Override
    public List<Student> queryClockStudent(long id) {
        List<Student> studentList = studentDao.queryClockStudent(id);
        return studentList;
    }

    @Override
    public List<Student> queryNoClockStudent(long id) {

        List<Student> students = studentDao.queryNoClockStudent(id);
        return students;
    }

    @Override
    public int querySus() {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("is_suspected",1);
        List<Student> students = baseMapper.selectList(wrapper);

        return students.size();
    }
    //导出学生信息
    @Override
    public void outPutStudentList(HttpServletResponse response) throws UnsupportedEncodingException {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生信息","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        List<Student> studentList = studentDao.selectList(null);
        ArrayList<StudentVo> voArrayList = new ArrayList<>();
        for (Student student:studentList
             ) {
            StudentVo studentVo = new StudentVo();
            studentVo.setId(student.getId());
            studentVo.setSname(student.getSname());
            studentVo.setSsex(student.getSsex());
            Long dormId = student.getDormId();
            String dormNameById = studentDao.queryDormNameById(dormId);
            studentVo.setDormName(dormNameById);
            voArrayList.add(studentVo);
        }
        try {
            EasyExcel.write(response.getOutputStream(),StudentVo.class).sheet("students")
                        .doWrite(voArrayList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
